<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>
    <base href="<%=basePath%>">
    <title>下级单位列表</title>
    <script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
    <script src="<%=basePath%>component/grid.js"></script>
    <script src='<%=basePath%>dwrsystem/interface/unitService.js'></script>
	<script src='<%=basePath%>dwrsystem/engine.js'></script>
	<script type="text/javascript">
	var mygrid;
	var unitId;
	var myUnittree;
	var dhxLayout = createLayoutNormal('1C',false,true);
	
	$(function (){
		unitId = '${param.superUnitId}';
		configLayout();
	 	dhxGrid.initDataTable();
		myUnittree = parent.parent.window.MyTree;
	});
	
	function configLayout(){
		dhxLayout.progressOn();
		var a = dhxLayout.cells('a');
		a.hideHeader();
		a.setText('主页面');
		a.fixSize(true, true);
		mygrid = createGridFromLayoutCell(a);
		if(power.getPower() != '')
			createToolbarFromDHXLayoutCell(a,function(toolbar){
			    toolbar.addText("info", 1, "操作:");
			    toolbar.addSeparator("sep1", 2);
			    if(power.getPowerInsert())toolbar.addButton("add", 3, "新增下级单位", "new.gif", "new_dis.gif");
			    if(power.getPowerDelete())toolbar.addButton("delete", 4, "删除", "cut.gif", "cut_dis.gif");
			    if(power.getPowerUpdate()){
			    	toolbar.addButton("updateUp", 5, "上移", "arrow_up.png", "arrow_up.png");
			    	toolbar.addButton("updateDown", 6, "下移", "arrow_down.png", "arrow_down.png");
			    };
			    toolbar.attachEvent("onClick", function(id) {
			    	if(id == 'add'){
			    		addUnit();
			    	}
			    	if(id == 'delete'){
			    		dhxGrid.deleteUnit();
			    	}
			    	if(id == 'updateUp'){
			    		dhxGrid.rowUp();
			    	}
			    	if(id == 'updateDown'){
			    		dhxGrid.rowDown();
			    	}
			    });
			});
	}
	
	var dhxGrid = {
		initDataTable : function () {
		    mygrid.setHeader("序号,单位名称,单位状态,备注");
		    mygrid.setInitWidths("50,200,150,*");
		    mygrid.setColAlign("right,left,left,left");
		    mygrid.setColSorting("int,str,str,str");
		    mygrid.setColTypes("ro,ro,ro,ro");
		    mygrid.attachEvent("onRowDblClicked",this.openSubUnit);
		    mygrid.attachEvent("onEnter",this.openSubUnit);
		    mygrid.init();
		    setSkin(mygrid);
		    mygrid.loadXML('<%=basePath%>system-unit/UnitAction!unitGrid.action?tunit.superUnitId=' + unitId,function(data){
			    dhxLayout.progressOff();
		    });
		},
		openSubUnit : function(rowID,celInd){
			myUnittree.selectNode(rowID);
		},
		deleteUnit : function (){
			if(mygrid.getSelectedId()){
				confirmInfo('确认删除？',
					function(choose){
						if('ok' == choose){
							layoutProgressOn(dhxLayout);
							unitService.deleteTUnit(mygrid.getSelectedId(),function(data){
								myUnittree.delNodeTop(mygrid.getSelectedId());
								var lastrowIndex = mygrid.getRowIndex(mygrid.getSelectedId())-1;
								mygrid.deleteSelectedRows();
								mygrid.selectRow(lastrowIndex);
								
								//修改前面的序号
								for(var i=(lastrowIndex+1) ;i<(mygrid.getRowsNum());i++){
									mygrid.cells(mygrid.getRowId(''+i),0).setValue(i+1);
								}
								layoutProgressOff(dhxLayout);
								parent.parent.parent.notifyCreater('删除成功');
							});
						}else{
							layoutProgressOff(dhxLayout);
							return;
						}
					}
				);
			} else parent.parent.parent.notifyWarn('请选择删除项');
		},
		rowUp:function(){
			var rowId = mygrid.getSelectedId();
			if(rowId) {
				this.sort('up',rowId);
			}else{
				parent.parent.parent.notifyWarn('请选择移动项');
			}
		},
		rowDown:function(){
			var rowId = mygrid.getSelectedId();
			if(rowId){
				this.sort('down',rowId);
			}else{
				parent.parent.parent.notifyWarn('请选择移动项');
			}
		},
		sort :function (status, id){
			var superrId = unitId;
	        var rInd = mygrid.getRowId((status=='up'?(mygrid.getRowIndex(id)-1):(mygrid.getRowIndex(id)+1)));
	        if(!rInd)	return ;
	        layoutProgressOn(dhxLayout);
			unitService.selectTUnitSortNum({unitId:rInd, superUnitId:superrId}, function(data){
				if(data != null){
					unitService.sortUnit(data, id);
					if(status == 'up'){
						mygrid.moveRowUp(id);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)),0).setValue(mygrid.getRowIndex(id)+1);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)+1),0).setValue(mygrid.getRowIndex(id)+2);
						myUnittree.moveUpStrict(id);
					}
					else{
						mygrid.moveRowDown(id);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)-1),0).setValue(mygrid.getRowIndex(id));
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)),0).setValue(mygrid.getRowIndex(id)+1);
						myUnittree.moveDownStrict(id);
					}
					parent.parent.parent.notifyCreater('排序成功');
					layoutProgressOff(dhxLayout);
				}else{
					parent.parent.parent.notifyWarn('排序发生错误');layoutProgressOff(dhxLayout);
					return;
				}
			});
		}
	};
	
	function addUnit(){
		layoutProgressOn(dhxLayout);
		document.location.href = '<%=basePath%>page/system/unit/unit_add.jsp?unitId=' + unitId + '&nowurl=list';
	}
		
	</script>	
</head>
  
<body>
</body>

</html>
